<?php

namespace App\Jobs\Posts;

use App\Models\Posts\WxComment;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class UpdateCommentOnlyTextJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $comment_id = 0;
    public $tries = 1;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($comment_id)
    {
        //
        $this->comment_id = $comment_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        if(_empty_($this->comment_id)){
            $comment_ids = WxComment::whereNull('is_only_text')->orderBy('id', 'desc')->limit(50)->pluck('id')->toArray();
            if($comment_ids){
                $is_noly_comments = [];
                $not_noly_comments = [];
                foreach ($comment_ids as $comment_id){
                    $comment = WxComment::find($comment_id);
                    if(mb_strlen($comment->comment_content) == mb_strlen(strip_tags($comment->comment_content))){
                        $is_noly_comments[] = $comment_id;
                    }else{
                        $not_noly_comments[] = $comment_id;
                    }
                }
                if(!_empty_($is_noly_comments)){
                    WxComment::whereIn('id', $is_noly_comments )->update(['is_only_text'=>1]);
                }
                if(!_empty_($not_noly_comments)){
                    WxComment::whereIn('id', $not_noly_comments )->update(['is_only_text'=>0]);
                }
            }
        }else{
            $comment = WxComment::find($this->comment_id);
            if($comment){
                if(mb_strlen($comment->comment_content) == mb_strlen(strip_tags($comment->comment_content))){
                    WxComment::where('id', $this->comment_id )->update(['is_only_text'=>1]);
                }else{
                    WxComment::where('id', $this->comment_id )->update(['is_only_text'=>0]);
                }
            }
        }
    }
}
